package career_service

import (
	"github.com/gin-gonic/gin"
	"peilian-api/app/global/databases/tables"
	"peilian-api/app/global/variable"
	"peilian-api/app/model"
)

type GetDiscussionReq struct {
	Page int `form:"page,default=0"`
	Size int `form:"size,default=10"`
}

type GetDiscussionResp struct {
	Total int64              `json:"total"`
	Data  []model.CareerResp `json:"data"`
}

func GetDiscussionList(ctx *gin.Context, req GetDiscussionReq) (*GetDiscussionResp, error) {
	agentId, _ := ctx.Get("aid")
	var career []tables.Career
	q := variable.DB.MySQL.Model(&tables.Career{}).Where(
		"career_type = 'discussion' and is_available <> ? and agent_id = ?", 2, agentId,
	).Order("order_id asc")
	var total int64
	err := q.Count(&total).Error
	if err != nil {
		return nil, err
	}

	if req.Size == 0 {
		req.Size = 10
	}
	offset := (req.Page - 1) * req.Size
	err = q.Offset(offset).Limit(req.Size).Find(&career).Error
	if err != nil {
		return nil, err
	}
	out := make([]model.CareerResp, 0)
	for i := 0; i < len(career); i++ {
		var care model.CareerResp
		care.Id = career[i].ID
		care.Name = career[i].Name
		care.CareerPhoto = career[i].CareerPhoto
		care.CareerMsg = *career[i].CareerMsg
		care.IsAvailable = career[i].IsAvailable
		care.Type = career[i].Type
		care.IsRelease = career[i].IsRelease
		care.PhotoIsDefault = career[i].PhotoIsDefault
		care.AgentId = career[i].AgentId
		out = append(out, care)
	}
	resp := &GetDiscussionResp{
		Total: total,
		Data:  out,
	}
	return resp, nil
}
